﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace YubicoProvider
{
    class WebServiceManager
    {
        private const string YUBICO_SERVER_URL = "http://api.yubico.com/wsapi/verify";
        private const string RESPONSE_STATUS_TEXT = "status=";
        public WebServiceManager(string verifyID)
        {
            this.VerifyID = verifyID;
        }

        public string VerifyID { get; private set; }

        public bool Verify(string oneTimePassword, out string message)
        {
            message = "Verification failed for an unkonwn reason.";
            bool returnValue = false;
            HttpWebRequest yubicoRequest = null;
            HttpWebResponse yubicoResponse = null;
            
            try
            {
                string yubicoValidationStatus = string.Empty;
                string yubicoURL = String.Format("{0}?id={1}&otp={2}", YUBICO_SERVER_URL, VerifyID, oneTimePassword);
                yubicoRequest = (HttpWebRequest)WebRequest.Create(yubicoURL);
                yubicoResponse = (HttpWebResponse)yubicoRequest.GetResponse();

                if (yubicoResponse.StatusCode == HttpStatusCode.OK)
                {
                    System.IO.StreamReader responseReader = new System.IO.StreamReader(yubicoResponse.GetResponseStream());
                    while (responseReader.Peek() > -1)
                    {
                        string responseLine = responseReader.ReadLine();
                        if (responseLine.ToLower().StartsWith(RESPONSE_STATUS_TEXT))
                        {
                            yubicoValidationStatus = responseLine.Substring(RESPONSE_STATUS_TEXT.Length, responseLine.Length - RESPONSE_STATUS_TEXT.Length);

                            if (yubicoValidationStatus == "OK")
                            {
                                message = String.Format("Verification successfull.", yubicoValidationStatus);
                                returnValue = true;
                            }
                            else
                            {
                                message = String.Format("Verification failed.  The service returned the following status: {0}", yubicoValidationStatus);
                            }

                            break;
                        }

                    }
                }
                else
                {
                    message = String.Format("The server failed to respond to the authentication request.{0}Server Response: {1}", Environment.NewLine, yubicoResponse.StatusCode.ToString());
                }
            }
            catch (Exception ex)
            {
                message = ex.ToString();
            }
            finally
            {

                if (yubicoRequest != null)
                    yubicoRequest = null;

                if (yubicoResponse != null)
                {
                    yubicoResponse.Close();
                    yubicoResponse = null;
                }
            }

            return returnValue;
        }
    }
}
